from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from autoTest_ecshop.utils.log_util import logger
import traceback


class BasePage:
    def __init__(self, driver):
        self.driver = driver
        self.wait = WebDriverWait(driver, 10)

    # 定位单个元素
    def find_element(self,locator):
        try:
            logger.info(f"定位元素：{locator}")
            element = self.wait.until(EC.presence_of_element_located(locator))
            logger.info(f"元素定位成功")
            return element
        except:
            logger.error(f"元素定位失败，失败原因如下：\n{traceback.print_exc()}")
            # 停止程序运行
            raise

    # 定位多个元素
    def find_elements(self, locator):
        try:
            logger.info(f"定位元素组：{locator}")
            elements = self.wait.until(EC.visibility_of_all_elements_located(locator))
            logger.info(f"元素组定位成功")
        except:
            logger.error(f"元素组定位失败，失败原因如下：\n{traceback.print_exc()}")
            # 停止程序运行
            raise

    # 输入数据
    def input_data(self,locator,value):
        # 定位元素
        inputBox = self.find_element(locator)
        inputBox.clear()
        logger.info(f"输入的数据是:{value}")
        inputBox.send_keys(value)

    # 点击按钮
    def btn_click(self,locator):
        # 定位元素
        inputBox = self.find_element(locator)
        logger.info("点击了按钮")
        inputBox.click()

    # 获取元素内容
    def element_text(self,locator):
        # 定位元素
        inputBox = self.find_element(locator)
        res =inputBox.text
        logger.info(f'获取到的内容为:{res}')
        return res











